package com.jeffse.light.stream.core.utils;

import com.jeffse.light.stream.core.thread.LightStreamThread;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

/**
 * 线程工具
 */
@Slf4j
public class ThreadUtils implements Serializable {
    private static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(0, Integer.MAX_VALUE,
            60L, TimeUnit.SECONDS,
            new SynchronousQueue<Runnable>());

    public static ThreadMXBean tmx = ManagementFactory.getThreadMXBean();

    public static Map<String, List<LightStreamThread>> threadMap = new HashMap();

    public static ThreadPoolExecutor getThreadPool(){
        return threadPoolExecutor;
    }

    /**
     * 执行线程
     * @param func
     */
    public static void execute(LightStreamThread func, String threadName){
        ThreadUtils.getThreadPool().execute(func);
        if(!threadMap.containsKey(threadName)){
            threadMap.put(threadName, new ArrayList<>());
        }

        threadMap.get(threadName).add(func);
        log.info("--------------------------  活动的线程： " + threadPoolExecutor.getActiveCount());
    }

    /**
     * 终止线程
     * @param threadName
     */
    public static void cancel(String threadName){
        log.error("--------------------------  活动的线程： " + threadPoolExecutor.getActiveCount());
    }

    public static Integer getActiveCount(){
        return threadPoolExecutor.getActiveCount();
    }

    /**
     * 获取所有线程信息
     * @return
     */
    public static List<ThreadInfo> getThreadsInfo(String threadName){
        List<ThreadInfo> list = new ArrayList<>();
        for(LightStreamThread lightStreamThread : threadMap.get(threadName)){
//            log.error("----------- " + lightStreamThread.getId());
//            log.error("----------- " + tmx.getThreadInfo(lightStreamThread.getId()).toString());
////            lightStreamThread.getT
//            list.add(tmx.getThreadInfo(lightStreamThread.getId()));
//            lightStreamThread.
//            Runtime.getRuntime().
        }

        return list;
    }

    /**
     * 获取所有线程信息
     * @return
     */
    public static List<ThreadInfo> getAllThreadsInfo(){
        List<ThreadInfo> list = new ArrayList<>();
        for(Long id : tmx.getAllThreadIds()){
            list.add(tmx.getThreadInfo(id));
        }

        return list;
    }

    /**
     * 获取僵死线程信息
     * @return
     */
    public static List<ThreadInfo> getDeadThreadsInfo(String threadName){
        List<ThreadInfo> list = new ArrayList<>();
        for (long id : tmx.findDeadlockedThreads()) {
            ThreadInfo ti = tmx.getThreadInfo(id);
            if(ti.getThreadName().equals(threadName)){
                list.add(ti);
            }
        }
        return list;
    }



    /**
     * 批量执行线程
     */
    public static void batchExecute() throws Exception {

    }
}
